package com.binance.healthcenter.telegram.command;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.meta.api.objects.Update;
import com.binance.healthcenter.masterelection.MasterElection;
import com.binance.healthcenter.model.HealthCheckResult;
import com.binance.healthcenter.notification.TelegramNotificationService;
import com.binance.healthcenter.service.DiscoveryClientService;
import com.binance.healthcenter.service.HealthCheckService;
import com.binance.master.utils.JsonUtils;
import lombok.extern.log4j.Log4j2;

@Log4j2
@TelegrambotCommandMatcher("/gethealth_*")
public class GetHealthCommand implements TelegrambotCommand {
    @Autowired
    private TelegramNotificationService telegramNotificationService;
    @Autowired
    private MasterElection masterElection;
    @Autowired
    private DiscoveryClientService discoveryClientService;
    @Autowired
    private HealthCheckService healthCheckService;


    @Override
    public void execute(Update update) {
        String text = update.getMessage().getText();
        try {
            // 解析指令
            log.info("GetHealthCommand.execute :text={}", text);
            List<String> serviceNameList = discoveryClientService.getServiceNameList();
            String currentServiceName = null;
            String requestServiceName = text.split("@")[0].replace("/gethealth_", "").trim();
            log.info("GetHealthCommand.execute start:text={},requestServiceName={}", text, requestServiceName);
            for (String serviceName : serviceNameList) {
                if (serviceName.equalsIgnoreCase(requestServiceName)) {
                    currentServiceName = serviceName;
                }
            }
            // 无效的命令，不处理
            if (StringUtils.isBlank(currentServiceName)) {
                log.info("GetHealthCommand.execute start:text={} empty", text);
                return;
            }
            log.info("GetHealthCommand.execute start:text={},currentServiceName={}", text, currentServiceName);
            // 获取某个微服务的健康检查结果
            List<HealthCheckResult> healthCheckResultList =
                    healthCheckService.getHealthInfoByServiceName(currentServiceName);
            log.info("GetHealthCommand.execute end:text={},currentServiceName={},healthCheckResultList={}", text,
                    currentServiceName, JsonUtils.toJsonHasNullKey(healthCheckResultList));
            // 当前机器是master的话那么收到指令，我们就推送信息
            if (masterElection.isMaster() && CollectionUtils.isNotEmpty(healthCheckResultList)) {
                for(HealthCheckResult healthCheckResult:healthCheckResultList){
                    telegramNotificationService.sendTextMessage(JsonUtils.toJsonNotNullKey(healthCheckResult));
                }
            }
        } catch (Exception e) {
            log.error("GetHealthCommand.execute text=" + text + " exception:", e);
        }
    }

}
